home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / zip10ex.zip / FILEIO.C < prev    next >
C/C++ Source or Header  |  1991-10-03  |  44KB  |  1,660 lines

  1. /*
  2.  
  3.  Copyright (C) 1990,1991 Mark Adler, Richard B. Wales, and Jean-loup Gailly.
  4.  Permission is granted to any individual or institution to use, copy, or
  5.  redistribute this software so long as all of the original files are included
  6.  unmodified, that it is not sold for profit, and that this copyright notice
  7.  is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  fileio.c by Mark Adler.
  13.  */
  14.  
  15. #include "zip.h"
  16.  
  17. #include <time.h>
  18. #include <errno.h>
  19.  
  20. #ifdef S_IWUSR          /* For MINIX */
  21. #  ifdef S_IWRITE
  22. #    undef S_IWRITE
  23. #  endif /* S_IWRITE */
  24. #  define S_IWRITE S_IWUSR
  25. #endif /* S_IWUSR */
  26.  
  27. #ifdef MSDOS
  28. #  include <io.h>
  29. #  ifdef __TURBOC__
  30. #    include <dir.h>
  31. #  else /* !__TURBOC__ */
  32. #    include <direct.h>
  33. #  endif /* ?__TURBOC__ */
  34. #  define link rename
  35. #  ifdef OS2
  36. #    define MATCH shmatch
  37. #  else /* !OS2 */
  38. #    define MATCH dosmatch
  39. #  endif /* ?OS2 */
  40. #else /* !MSDOS */
  41.    extern int errno;    /* error number from system functions */
  42. #  ifdef VMS
  43. #    define RMDIR
  44. #    define link rename
  45. #  endif /* VMS */
  46. #  define MATCH shmatch
  47. #endif /* ?MSDOS */
  48.  
  49. #ifdef UTS
  50. #  define RMDIR
  51. #endif /* UTS */
  52.  
  53.  
  54. /* Extra malloc() space in names for cutpath() */
  55. #ifdef VMS
  56. #  define PAD 3         /* may have to change .FOO] to ]FOO.DIR */
  57. #else /* !VMS */
  58. #  define PAD 0
  59. #endif /* ?VMS */
  60.  
  61.  
  62. /* For now, assume DIRENT implies System V implies TERMIO */
  63. #ifdef DIRENT
  64. #  ifndef MINIX
  65. #    ifndef TERMIO
  66. #      define TERMIO
  67. #    endif /* !TERMIO */
  68. #  endif /* !MINIX */
  69. #endif /* DIRENT */
  70.  
  71.  
  72. #ifndef EXPORT
  73. #  ifdef MSVMS
  74. #    ifdef MSDOS
  75. #      include <conio.h>
  76. #    else /* !MSDOS */
  77. #      define getch() getc(stderr)
  78. #    endif /* ?MSDOS */
  79. #  else /* !MSVMS */
  80. #    ifdef TERMIO       /* Amdahl, Cray, all SysV? */
  81. #      ifdef CONVEX
  82. #        include <sys/termios.h>
  83. #        include <sgtty.h>
  84. #      else /* !CONVEX */
  85. #        include <sys/termio.h>
  86. #        define sgttyb termio
  87. #        define sg_flags c_lflag
  88. #      endif /* ?CONVEX */
  89.        int ioctl OF((int, int, voidp *));
  90. #      define GTTY(f,s) ioctl(f,TCGETA,s)
  91. #      define STTY(f,s) ioctl(f,TCSETAW,s)
  92. #    else /* !TERMIO */
  93. #      ifndef MINIX
  94. #        include <sys/ioctl.h>
  95. #      endif /* !MINIX */
  96. #      include <sgtty.h>
  97.        int gtty OF((int, struct sgttyb *));
  98.        int stty OF((int, struct sgttyb *));
  99. #      define GTTY gtty
  100. #      define STTY stty
  101. #    endif /* ?TERMIO */
  102.      int isatty OF((int));
  103.      char *ttyname OF((int));
  104.      int open OF((char *, int, ...));
  105.      int close OF((int));
  106.      int read OF((int, voidp *, int));
  107. #  endif /* ?MSVMS */
  108. #endif /* !EXPORT */
  109.  
  110.  
  111.  
  112. /* For directory access */
  113. #ifndef UTIL
  114. #ifdef DIRENT                   /* use getdents() */
  115. #  ifdef MINIX
  116. #    include <dirent.h>
  117. #  else /* !MINIX */
  118. #    include <sys/dirent.h>
  119. #  endif /* ?MINIX */
  120. #  define direct dirent
  121. #  ifdef MINIX
  122.      int getdents OF((int, char *, unsigned));
  123. #  else /* !MINIX */
  124.      int getdents OF((int, char *, int));
  125. #  endif /* ?MINIX */
  126. #  define DBSZ 4096     /* This has to be bigger than a directory block */
  127.    typedef struct {     /* directory stream buffer */
  128.      int f;             /* file descriptor for the directory "file" */
  129.      char *p;           /* pointer to next entry in buffer */
  130.      char *q;           /* pointer after end of buffer contents */
  131.      char b[DBSZ];              /* buffer */
  132.    } dstrm;
  133. #else /* !DIRENT */             /* use opendir(), etc. */
  134. #  ifdef CONVEX
  135. #    include <dirent.h>
  136. #    define direct dirent
  137. #  endif /* CONVEX */
  138. #  ifdef NDIR
  139. #    include "ndir.h"           /* for HPUX */
  140. #  else /* !NDIR */
  141. #    ifdef MSDOS
  142. #     ifdef OS2
  143. #      include "dir_os2.h"
  144. #     else /* !OS2 */
  145. #      include <dos.h>
  146. #      ifdef __TURBOC__
  147. #        define FATTR           FA_HIDDEN+FA_SYSTEM+FA_DIREC
  148. #        define FFIRST(n,d)     findfirst(n,(struct ffblk *)d,FATTR)
  149. #        define FNEXT(d)        findnext((struct ffblk *)d)
  150. #      else /* !__TURBOC__ */
  151. #        define FATTR           _A_HIDDEN+_A_SYSTEM+_A_SUBDIR
  152. #        define FFIRST(n,d)     _dos_findfirst(n,FATTR,(struct find_t *)d)
  153. #        define FNEXT(d)        _dos_findnext((struct find_t *)d)
  154. #      endif /* ?__TURBOC__ */
  155.        typedef struct direct {
  156.          char d_reserved[30];
  157.          char d_name[13];
  158.          int d_first;
  159.        } DIR;
  160. #     endif /* ?OS2 */
  161. #    else /* !MSDOS */
  162. #      ifdef VMS
  163. #        include <rms.h>
  164. #        include <ssdef.h>
  165. #        include <descrip.h>
  166.          typedef struct direct {
  167.              int d_wild;                /* flag for wildcard vs. non-wild */
  168.              struct FAB fab;
  169.              struct NAM nam;
  170.              char d_qualwildname[NAM$C_MAXRSS + 1];
  171.              char d_name[NAM$C_MAXRSS + 1];
  172.          } DIR;
  173. #      else /* !VMS */
  174. #        include <sys/dir.h>
  175. #        ifdef NODIR                    /* for AT&T 3B1 */
  176. #          define dirent direct
  177.            typedef FILE DIR;
  178. #          define dstrm DIR
  179. #        endif /* NODIR */
  180. #      endif /* ?VMS */
  181. #    endif /* ?MSDOS */
  182. #  endif /* ?NDIR */
  183. #  define dstrm DIR
  184. #  ifndef NODIR
  185.      DIR *opendir OF((char *));
  186. #  endif /* !NODIR */
  187. #  ifndef CONVEX
  188.      struct direct *readdir OF((DIR *));
  189. #  endif /* !CONVEX */
  190. #endif /* ?DIRENT */
  191. #endif /* !UTIL */
  192.  
  193.  
  194. /* Library functions not in (most) header files */
  195. char *mktemp OF((char *));
  196. int link OF((char *, char *));
  197. int unlink OF((char *));
  198. #ifndef CONVEX
  199. #  ifndef AIX
  200.      int chmod OF((char *, int));
  201. #  endif /* !AIX */
  202. #endif /* !CONVEX */
  203.  
  204.  
  205. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  206.  
  207. #ifndef __TURBOC__
  208.    int utime OF((char *, time_t *));
  209. #endif /* !__TURBOC__ */
  210. #ifndef MSDOS
  211.    int open OF((char *, int, ...));
  212.    int close OF((int));
  213. #  ifndef RMDIR
  214.      int rmdir OF((char *));
  215. #  endif /* !RMDIR */
  216. #endif /* !MSDOS */
  217.  
  218.  
  219. /* Local globals (kinda like "military intelligence" or "broadcast quality") */
  220. local int exflag = 0;           /* Exclude flag */
  221.  
  222. /* Local functions */
  223. #ifdef PROTO
  224. #  ifdef VMS
  225.      local void vms_wild(char *, dstrm *);
  226. #  endif /* VMS */
  227. #  ifdef DIRENT
  228.      local dstrm *opend(char *);
  229.      local void closed(dstrm *);
  230. #  endif /* DIRENT */
  231.    local char *readd(dstrm *);
  232.    local int fqcmp(voidp *, voidp *);
  233.    local int fqcmpz(voidp *, voidp *);
  234.    local char *last(char *);
  235.    local char *msname(char *);
  236. #  ifdef VMS
  237.      local char *strlower(char *);
  238.      local char *strupper(char *);
  239. #  endif /* VMS */
  240.    local char *ex2in(char *);
  241.    local int newname(char *);
  242.    local void inctime(struct tm *);
  243.    local ulg unix2dostime(time_t *);
  244. #  ifndef __TURBOC__
  245.      local int cmptime(struct tm *, struct tm *);
  246.      local time_t invlocal(struct tm *);
  247. #  endif /* !__TURBOC__ */
  248. #endif /* PROTO */
  249.  
  250.  
  251.  
  252. #ifndef OS2
  253. #ifdef MSDOS
  254. dstrm *opendir(n)
  255. char *n;                /* directory to open */
  256. /* Start searching for files in the MSDOS directory n */
  257. {
  258.   dstrm *d;             /* malloc'd return value */
  259.   char *p;              /* malloc'd temporary string */
  260.  
  261.   if ((d = (dstrm *)malloc(sizeof(dstrm))) == NULL ||
  262.       (p = malloc(strlen(n) + 5)) == NULL)
  263.     return NULL;
  264.   strcat(strcpy(p, n), "/*.*");
  265.   if (FFIRST(p, d))
  266.   {
  267.     free((voidp *)p);
  268.     return NULL;
  269.   }
  270.   free((voidp *)p);
  271.   d->d_first = 1;
  272.   return d;
  273. }
  274.  
  275. struct direct *readdir(d)
  276. dstrm *d;               /* directory stream to read from */
  277. /* Return pointer to first or next directory entry, or NULL if end. */
  278. {
  279.   if (d->d_first)
  280.     d->d_first = 0;
  281.   else
  282.     if (FNEXT(d))
  283.       return NULL;
  284.   return (struct direct *)d;
  285. }
  286. #  define closedir free
  287. #endif /* MSDOS */
  288. #endif /* !OS2 */
  289.  
  290.  
  291. #ifdef VMS
  292. /*---------------------------------------------------------------------------
  293.  
  294.     _vms_findfirst() and _vms_findnext(), based on public-domain DECUS C
  295.     fwild() and fnext() routines (originally written by Martin Minow, poss-
  296.     ibly modified by Jerry Leichter for bintnxvms.c), were written by Greg
  297.     Roelofs and are still in the public domain.  Routines approximate the
  298.     behavior of MS-DOS (MSC and Turbo C) findfirst and findnext functions.
  299.  
  300.   ---------------------------------------------------------------------------*/
  301. local void vms_wild(p, d)
  302. char *p;
  303. dstrm *d;
  304. {
  305.   /*
  306.    * Do wildcard setup
  307.    */
  308.   /* set up the FAB and NAM blocks. */
  309.   d->fab = cc$rms_fab;             /* initialize fab */
  310.   d->nam = cc$rms_nam;             /* initialize nam */
  311.  
  312.   d->fab.fab$l_nam = &d->nam;           /* fab -> nam */
  313.   d->fab.fab$l_fna = p;                 /* argument wild name */
  314.   d->fab.fab$b_fns = strlen(p);         /* length */
  315.  
  316.   d->nam.nam$l_esa = d->d_qualwildname; /* qualified wild name */
  317.   d->nam.nam$b_ess = NAM$C_MAXRSS;      /* max length */
  318.   d->nam.nam$l_rsa = d->d_name;         /* matching file name */
  319.   d->nam.nam$b_rss = NAM$C_MAXRSS;      /* max length */
  320.  
  321.   /* parse the file name */
  322.   if (sys$parse(&d->fab) != RMS$_NORMAL)
  323.     return -1;
  324.   /* Does this replace d->fab.fab$l_fna with a new string in its own space?
  325.      I sure hope so, since p is free'ed before this routine returns. */
  326.  
  327.   /* have qualified wild name (i.e., disk:[dir.subdir]*.*); null-terminate
  328.    * and set wild-flag */
  329.   d->d_qualwildname[d->nam.nam$b_esl] = '\0';
  330.   d->d_wild = (d->nam.nam$l_fnb & NAM$M_WILDCARD)? 1 : 0;   /* not used... */
  331. #ifdef DEBUG
  332.   printf("  incoming wildname:  %s\n", p);
  333.   printf("  qualified wildname:  %s\n", d->d_qualwildname);
  334. #endif /* DEBUG */
  335. }
  336.  
  337. dstrm *opendir(n)
  338. char *n;                /* directory to open */
  339. /* Start searching for files in the VMS directory n */
  340. {
  341.   char *c;              /* scans VMS path */
  342.   dstrm *d;             /* malloc'd return value */
  343.   int m;                /* length of name */
  344.   char *p;              /* malloc'd temporary string */
  345.  
  346.   if ((d = (dstrm *)malloc(sizeof(dstrm))) == NULL ||
  347.       (p = malloc((m = strlen(n)) + 4)) == NULL)
  348.     return NULL;
  349.   /* Directory may be in form "[DIR.SUB1.SUB2]" or "[DIR.SUB1]SUB2.DIR;1".
  350.      If latter, convert to former. */
  351.   if (m > 0  &&  *(c = strcpy(p,n)+m-1) != ']')
  352.   {
  353.     while (--c > p  &&  *c != ';')
  354.       ;
  355.     if (c-p < 5  ||  strncmp(c-4, ".DIR", 4))
  356.     {
  357.       free((voidp *)d);  free((voidp *)p);
  358.       return NULL;
  359.     }
  360.     c -= 3;
  361.     *c-- = '\0';        /* terminate at "DIR;#" */
  362.     *c = ']';           /* "." --> "]" */
  363.     while (c > p  &&  *--c != ']')
  364.       ;
  365.     *c = '.';           /* "]" --> "." */
  366.   }
  367.   strcat(p, "*.*");
  368.   vms_wild(p, d);       /* set up wildcard */
  369.   free((voidp *)p);
  370.   return d;
  371. }
  372.  
  373. struct direct *readdir(d)
  374. dstrm *d;               /* directory stream to read from */
  375. /* Return pointer to first or next directory entry, or NULL if end. */
  376. {
  377.   int r;                /* return code */
  378.  
  379.   do {
  380.     d->fab.fab$w_ifi = 0;       /* internal file index:  what does this do? */
  381.  
  382.     /* get next match to possible wildcard */
  383.     if ((r = sys$search(&d->fab)) == RMS$_NORMAL)
  384.     {
  385.         d->d_name[d->nam.nam$b_rsl] = '\0';   /* null terminate */
  386.         return (struct direct *)d;   /* OK */
  387.     }
  388.   } while (r == RMS$_PRV);
  389.   return NULL;
  390. }
  391. #  define closedir free
  392. #endif /* VMS */
  393.  
  394.  
  395. #ifdef NODIR                    /* for AT&T 3B1 */
  396. /*
  397. **  Apparently originally by Rich Salz.
  398. **  Cleaned up and modified by James W. Birdsall.
  399. */
  400.  
  401. #  define opendir(path) fopen(path, "r")
  402.  
  403. struct direct *readdir(dirp)
  404. DIR *dirp;
  405. {
  406.   static struct direct entry;
  407.  
  408.   if (dirp == NULL) 
  409.     return NULL;
  410.   for (;;)
  411.     if (fread (&entry, sizeof (struct direct), 1, dirp) == 0) 
  412.       return NULL;
  413.     else if (entry.d_ino) 
  414.       return (&entry);
  415. } /* end of readdir() */
  416.  
  417. #  define closedir(dirp) fclose(dirp)
  418. #endif /* NODIR */
  419.  
  420.  
  421. #ifdef DIRENT
  422. local dstrm *opend(n)
  423. char *n;                /* directory name to open */
  424. /* Open the directory *n, returning a pointer to an allocated dstrm, or
  425.    NULL if error. */
  426. {
  427.   dstrm *d;             /* pointer to malloc'ed directory stream */
  428.  
  429.   if ((d = (dstrm *)malloc(sizeof(dstrm))) == NULL)
  430.     return NULL;
  431.   if ((d->f = open(n, 0, 0)) < 0)               /* open directory */
  432.     return NULL;
  433.   d->p = d->q = d->b;                           /* buffer is empty */
  434.   return d;
  435. }
  436. #else /* !DIRENT */
  437. #  define opend opendir                         /* just use opendir() */
  438. #endif /* ?DIRENT */
  439.  
  440.  
  441. local char *readd(d)
  442. dstrm *d;               /* directory stream to read from */
  443. /* Return a pointer to the next name in the directory stream d, or NULL if
  444.    no more entries or an error occurs. */
  445. {
  446.   struct direct *e;     /* directory entry read */
  447.  
  448. #ifdef DIRENT
  449.   int n;                /* number of entries read by getdents */
  450.  
  451.   if (d->p >= d->q)                             /* if empty, fill buffer */
  452.     if ((n = getdents(d->f, d->b, DBSZ)) <= 0)
  453.       return NULL;
  454.     else
  455.       d->q = n + (d->p = d->b);
  456.   e = (struct direct *)(d->p);                  /* point to entry */
  457.   d->p += ((struct direct *)(d->p))->d_reclen;  /* advance */
  458.   return e->d_name;                             /* return name */
  459. #else /* !DIRENT */
  460.   return (e = readdir(d)) == NULL ? (char *)NULL : e->d_name;
  461. #endif /* ?DIRENT */
  462. }
  463.  
  464.  
  465. #ifdef DIRENT
  466. local void closed(d)
  467. dstrm *d;               /* directory stream to close */
  468. /* Close the directory stream */
  469. {
  470.   close(d->f);
  471.   free((voidp *)d);
  472. }
  473. #else /* !DIRENT */
  474. #  define closed closedir
  475. #endif /* ?DIRENT */
  476.  
  477.  
  478. #ifdef MSDOS
  479. int wild(p)
  480. char *p;                /* path/pattern to match */
  481. /* If not in exclude mode, expand the pattern based on the contents of the
  482.    file system.  Return an error code in the ZE_ class. */
  483. {
  484.   dstrm *d;             /* stream for reading directory */
  485.   char *e;              /* name found in directory */
  486.   int f;                /* true if there was a match */
  487.   char *n;              /* constructed name from directory */
  488.   char *q;              /* temporary variable */
  489.   int r;                /* temporary variable */
  490.   char v[4];            /* space for device current directory */
  491.  
  492.   /* If excluding, don't bother with file system */
  493.   if (exflag)
  494.     return procname(p);
  495.  
  496.   /* Normalize pattern to upper case, path delimiter as '/'. */
  497. #ifndef OS2
  498.   strupr(p);                            /* convert to upper case */
  499. #endif /* !OS2 */
  500.   for (q = p; *q; q++)                  /* use / consistently */
  501.     if (*q == '\\')
  502.       *q = '/';
  503.  
  504.   /* Only name can have special matching characters */
  505.   if ((q = isshexp(p)) != NULL &&
  506.       (strrchr(q, '/') != NULL || strrchr(q, ':') != NULL))
  507.     return ZE_PARMS;
  508.  
  509.   /* Separate path and name */
  510.   if ((q = strrchr(p, '/')) != NULL)
  511.     *q++ = 0;
  512.   else if (*p && *(p+1) == ':')
  513.   {
  514.     q = p + 2;
  515.     v[0] = *p;
  516.     strcpy(v+1, ":.");
  517.     p = v;
  518.   }
  519.   else
  520.   {
  521.     q = p;
  522.     p = ".";
  523.   }
  524.   if (*p == 0)
  525.     p = "/";
  526.  
  527.   /* Search that level for matching names */
  528.   if ((d = opend(p)) == NULL)
  529.     return ZE_MISS;
  530.   if (strcmp(p+1, ":.") == 0)
  531.     *(p+2) = 0;
  532.   f = 0;
  533.   while ((e = readd(d)) != NULL)
  534.     if (strcmp(e, ".") && strcmp(e, "..") && MATCH(q, e))
  535.     {
  536.       f = 1;
  537.       if (strcmp(p, ".") == 0)
  538.         procname(e);
  539.       else if (*p && strcmp(p+1, ":") == 0)
  540.       {
  541.         if ((n = malloc(strlen(e) + 3)) == NULL)
  542.           return ZE_MEM;
  543.         r = procname(strcat(strcpy(n, p), e));
  544.         free((voidp *)n);
  545.         if (r)
  546.           return r;
  547.       }
  548.       else
  549.       {
  550.         if ((n = malloc(strlen(p) + strlen(e) + 2)) == NULL)
  551.           return ZE_MEM;
  552.         if (strcmp(p, "/"))
  553.           strcpy(n, p);
  554.         else
  555.           *n = 0;
  556.         r = procname(strcat(strcat(n, "/"), e));
  557.         free((voidp *)n);
  558.         if (r)
  559.           return r;
  560.       }
  561.     }
  562.   closed(d);
  563.  
  564.   /* Done */
  565.   return f ? ZE_OK : ZE_MISS;
  566. }
  567. #endif /* MSDOS */
  568.  
  569.  
  570. #ifdef VMS
  571. int wild(p)
  572. char *p;                /* path/pattern to match */
  573. /* Expand the pattern based on the contents of the file system.  Return an
  574.    error code in the ZE_ class. */
  575. {
  576.   dstrm *d;             /* stream for reading directory */
  577.   char *e;              /* name found in directory */
  578.   int f;                /* true if there was a match */
  579.  
  580.   /* Search given pattern for matching names */
  581.   if ((d = (dstrm *)malloc(sizeof(dstrm))) == NULL)
  582.     return ZE_MEM;
  583.   vms_wild(p, d);       /* pattern may be more than just directory name */
  584.   f = 0;
  585.   while ((e = readd(d)) != NULL)        /* "dosmatch" is already built in */
  586.     if (procname(e) == ZE_OK)
  587.       f = 1;
  588.   closed(d);
  589.  
  590.   /* Done */
  591.   return f ? ZE_OK : ZE_MISS;
  592. }
  593. #endif /* VMS */
  594.  
  595.  
  596. char *getnam(n)
  597. char *n;                /* where to put name (must have >=FNMAX+1 bytes) */
  598. /* Read a space, \n, \r, or \t delimited name from stdin into n, and return
  599.    n.  If EOF, then return NULL.  Also, if the name read is too big, return
  600.    NULL. */
  601. {
  602.   int c;                /* last character read */
  603.   char *p;              /* pointer into name area */
  604.  
  605.   p = n;
  606.   while ((c = getchar()) == ' ' || c == '\n' || c == '\r' || c == '\t')
  607.     ;
  608.   if (c == EOF)
  609.     return NULL;
  610.   do {
  611.     if (p - n >= FNMAX)
  612.       return NULL;
  613.     *p++ = (char)c;
  614.     c = getchar();
  615.   } while (c != EOF && c != ' ' && c != '\n' && c != '\r' && c != '\t');
  616.   *p = 0;
  617.   return n;
  618. }
  619.  
  620.  
  621. struct flist far *fexpel(f)
  622. struct flist far *f;    /* entry to delete */
  623. /* Delete the entry *f in the doubly-linked found list.  Return pointer to
  624.    next entry to allow stepping through list. */
  625. {
  626.   struct flist far *t;  /* temporary variable */
  627.  
  628.   t = f->nxt;
  629.   *(f->lst) = t;                        /* point last to next, */
  630.   if (t != NULL)
  631.     t->lst = f->lst;                    /* and next to last */
  632.   if (f->name != NULL)                  /* free memory used */
  633.     free((voidp *)(f->name));
  634.   if (f->zname != NULL)
  635.     free((voidp *)(f->zname));
  636.   farfree((voidp far *)f);
  637.   fcount--;                             /* decrement count */
  638.   return t;                             /* return pointer to next */
  639. }
  640.  
  641.  
  642. local int fqcmp(a, b)
  643. voidp *a, *b;           /* pointers to pointers to found entries */
  644. /* Used by qsort() to compare entries in the found list by name. */
  645. {
  646.   return strcmp((*(struct flist far **)a)->name,
  647.                 (*(struct flist far **)b)->name);
  648. }
  649.  
  650.  
  651. local int fqcmpz(a, b)
  652. voidp *a, *b;           /* pointers to pointers to found entries */
  653. /* Used by qsort() to compare entries in the found list by zname. */
  654. {
  655.   return strcmp((*(struct flist far **)a)->zname,
  656.                 (*(struct flist far **)b)->zname);
  657. }
  658.  
  659.  
  660. local char *last(p)
  661. char *p;                /* sequence of / delimited path components */
  662. /* Return a pointer to the start of the last path component. */
  663. {
  664.   char *t;              /* temporary variable */
  665.  
  666. #ifdef VMS
  667.   if ((t = strrchr(p, ']')) != NULL)
  668. #else /* !VMS */
  669.   if ((t = strrchr(p, '/')) != NULL)
  670. #endif /* ?VMS */
  671.     return t + 1;
  672.   else
  673.     return p;
  674. }
  675.  
  676.  
  677. #define TOUP(c) ((c) >= 'a' && (c) <= 'z' ? (c)-'a'+'A' : (c))
  678.  
  679. local char *msname(n)
  680. char *n;
  681. /* Reduce all path components to MSDOS upper case 8.3 style names.  Probably
  682.    should also check for invalid characters, but I don't know which ones
  683.    those are. */
  684. {
  685.   int c;                /* current character */
  686.   int f;                /* characters in current component */
  687.   char *p;              /* source pointer */
  688.   char *q;              /* destination pointer */
  689.  
  690.   p = q = n;
  691.   f = 0;
  692.   while ((c = *p++) != 0)
  693.     if (c == '/')
  694.     {
  695.       *q++ = (char)c;
  696.       f = 0;                            /* new component */
  697.     }
  698.     else if (c == '.')
  699.       if (f < 9)
  700.       {
  701.         *q++ = (char)c;
  702.         f = 9;                          /* now in file type */
  703.       }
  704.       else
  705.         f = 12;                         /* now just excess characters */
  706.     else
  707.       if (f < 12 && f != 8)
  708.       {
  709.         *q++ = (char)(TOUP(c));
  710.         f++;                            /* do until end of name or type */
  711.       }
  712.   *q = 0;
  713.   return n;
  714. }
  715.  
  716.  
  717. #ifdef VMS
  718. local char *strlower(s)
  719. char *s;                /* string to convert */
  720. /* Convert all uppercase letters to lowercase in string s */
  721. {
  722.   char *p;              /* scans string */
  723.  
  724.   for (p = s; *p; p++)
  725.     if (*p >= 'A' && *p <= 'Z')
  726.       *p += 'a' - 'A';
  727.   return s;
  728. }
  729.  
  730. local char *strupper(s)
  731. char *s;                /* string to convert */
  732. /* Convert all lowercase letters to uppercase in string s */
  733. {
  734.   char *p;              /* scans string */
  735.  
  736.   for (p = s; *p; p++)
  737.     if (*p >= 'a' && *p <= 'a')
  738.       *p -= 'a' - 'A';
  739.   return s;
  740. }
  741. #endif /* VMS */
  742.  
  743.  
  744. local char *ex2in(x)
  745. char *x;                /* external file name */
  746. /* Convert the external file name to a zip file name, returning the malloc'ed
  747.    string or NULL if not enough memory. */
  748. {
  749.   char *n;              /* internal file name (malloc'ed) */
  750.   char *t;              /* shortened name */
  751.  
  752.   /* Find starting point in name before doing malloc */
  753. #ifdef MSDOS                            /* msdos */
  754.   t = *x && *(x + 1) == ':' ? x + 2 : x;
  755.   while (*t == '/' || *t == '\\')
  756.     t++;
  757. #else /* !MSDOS */
  758. #  ifdef VMS                            /* vms */
  759.   t = x;
  760.   if ((n = strrchr(t, ':')) != NULL)
  761.     t = n + 1;
  762.   if (*t == '[' && (n = strrchr(t, ']')) != NULL)
  763.     if ((x = strchr(t, '.')) != NULL && x < n)
  764.       t = x + 1;
  765.     else
  766.       t = n + 1;
  767. #  else /* !VMS */                      /* unix */
  768.   for (t = x; *t == '/'; t++)
  769.     ;
  770. #  endif /* ?VMS */
  771. #endif /* ?MSDOS */
  772.   if (!pathput)
  773.     t = last(t);
  774.  
  775.   /* Malloc space for internal name and copy it */
  776.   if ((n = malloc(strlen(t) + 1)) == NULL)
  777.     return NULL;
  778.   strcpy(n, t);
  779.  
  780.   /* Make changes, if any, to the copied name (leave original intact) */
  781. #ifdef MSDOS
  782.   for (t = n; *t; t++)
  783.     if (*t == '\\')
  784.       *t = '/';
  785. #endif /* MSDOS */
  786. #ifdef VMS
  787.   if ((t = strrchr(n, ']')) != NULL)
  788.   {
  789.     *t = '/';
  790.     while (--t > n)
  791.       if (*t == '.')
  792.         *t = '/';
  793.   }
  794.  
  795.   /* Fix from Greg Roelofs: */
  796.   /* Get current working directory and strip from n (t now = n) */
  797.   {
  798.     char cwd[256], *p, *q;
  799.     int c;
  800.  
  801.     if (getcwd(cwd, 256) && ((p = strchr(cwd, '.')) != NULL))
  802.     {
  803.       ++p;
  804.       if ((q = strrchr(p, ']')) != NULL)
  805.       {
  806.         *q = '/';
  807.         while (--q > p)
  808.           if (*q == '.')
  809.             *q = '/';
  810.         /* strip bogus path parts from n */
  811.         if (strncmp(n, p, (c=strlen(p))) == 0)
  812.         {
  813.           q = n + c;
  814.           while (*t++ = *q++)
  815.             ;
  816.         }
  817.       }
  818.     }
  819.   }
  820.   strlower(n);
  821.   if (!vmsver)
  822.     if ((t = strrchr(n, ';')) != NULL)
  823.       *t = 0;
  824. #endif /* VMS */
  825.   if (dosify)
  826.     msname(n);
  827.  
  828.   /* Returned malloc'ed name */
  829.   return n;
  830. }
  831.  
  832.  
  833. char *in2ex(n)
  834. char *n;                /* internal file name */
  835. /* Convert the zip file name to an external file name, returning the malloc'ed
  836.    string or NULL if not enough memory. */
  837. {
  838.   char *x;              /* external file name */
  839. #ifdef VMS
  840.   char *t;              /* scans name */
  841.  
  842.   if ((t = strrchr(n, '/')) == NULL)
  843. #endif /* VMS */
  844.   {
  845.     if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  846.       return NULL;
  847.     strcpy(x, n);
  848.   }
  849. #ifdef VMS
  850.   else
  851.   {
  852.     if ((x = malloc(strlen(n) + 3 + PAD)) == NULL)
  853.       return NULL;
  854.     strcpy(x, "[.");
  855.     strcpy(x + 2, n);
  856.     *(t = x + 2 + (t - n)) = ']';
  857.     while (--t > x)
  858.       if (*t == '/')
  859.         *t = '.';
  860.   }
  861.   strupper(x);
  862. #endif /* VMS */
  863.   return x;
  864. }
  865.  
  866.  
  867. int exclude()
  868. /* Change from including to excluding names when procname() called.  Return
  869.    an error code in the ZE_ class. */
  870. {
  871.   struct flist far *f;          /* steps through found linked list */
  872.   int j;                        /* index for s */
  873.   struct flist far **s;         /* sorted table */
  874.  
  875.   /* sort found list, remove duplicates */
  876.   if (fcount)
  877.   {
  878.     if ((s = (struct flist far **)malloc(
  879.          fcount * sizeof(struct flist far *))) == NULL)
  880.       return ZE_MEM;
  881.     for (j = 0, f = found; f != NULL; f = f->nxt)
  882.       s[j++] = f;
  883.     qsort((char *)s, fcount, sizeof(struct flist far *), fqcmp);
  884.     for (j = fcount - 1; j > 0; j--)
  885.       if (strcmp(s[j - 1]->name, s[j]->name) == 0)
  886.         fexpel(s[j]);           /* fexpel() changes fcount */
  887.     qsort((char *)s, fcount, sizeof(struct flist far *), fqcmpz);
  888.     for (j = 1; j < fcount; j++)
  889.       if (strcmp(s[j - 1]->zname, s[j]->zname) == 0)
  890.       {
  891.         warn("name in zip file repeated: ", s[j]->zname);
  892.         warn("  first full name: ", s[j - 1]->name);
  893.         warn(" second full name: ", s[j]->name);
  894.         return ZE_PARMS;
  895.       }
  896.     free((voidp *)s);
  897.   }
  898.   exflag = 1;
  899.   return ZE_OK;
  900. }
  901.  
  902.  
  903. local int newname(n)
  904. char *n;                /* name to add (or exclude) */
  905. /* Add (or exclude) a name that is not in the zip file.  Return an error
  906.    code in the ZE_ class. */
  907. {
  908.   char *m;
  909.   struct flist far *f;  /* where in found, or new found entry */
  910.   struct zlist far *z;  /* where in zfiles (if found) */
  911.  
  912.   /* Search for name in zip file.  If there, mark it, else add to
  913.      list of new names to do (or remove from that list). */
  914.   if ((m = ex2in(n)) == NULL)
  915.     return ZE_MEM;
  916.   if ((z = zsearch(m)) != NULL)
  917.     if (exflag)
  918.     {
  919.       z->mark = 0;
  920.       free((voidp *)m);
  921.       if (verbose)
  922.         printf("zip diagnostic: excluding %s\n", z->name);
  923.     }
  924.     else
  925.     {
  926.       free((voidp *)(z->name));
  927.       free((voidp *)(z->zname));
  928.       if ((z->name = malloc(strlen(n) + 1 + PAD)) == NULL)
  929.         return ZE_MEM;
  930.       strcpy(z->name, n);
  931.       z->zname = m;
  932.       z->mark = 1;
  933.       if (verbose)
  934.         printf("zip diagnostic: including %s\n", z->name);
  935.     }
  936.   else
  937.     if (exflag)
  938.     {
  939.       /* search list for name--if there, remove it */
  940.       for (f = found; f != NULL; f = f->nxt)
  941.         if (strcmp(n, f->name) == 0)
  942.         {
  943.           fexpel(f);
  944.           break;
  945.         }
  946.       free((voidp *)m);
  947.     }
  948.     else
  949.     {
  950.       /* allocate space and add to list */
  951.       if ((f = (struct flist far *)farmalloc(sizeof(struct flist))) == NULL ||
  952.           (f->name = malloc(strlen(n) + 1 + PAD)) == NULL)
  953.       {
  954.         if (f != NULL)
  955.           farfree((voidp far *)f);
  956.         return ZE_MEM;
  957.       }
  958.       strcpy(f->name, n);
  959.       f->zname = m;
  960.       *fnxt = f;
  961.       f->lst = fnxt;
  962.       f->nxt = NULL;
  963.       fnxt = &f->nxt;
  964.       fcount++;
  965.     }
  966.   return ZE_OK;
  967. }
  968.  
  969.  
  970. int procname(n)
  971. char *n;                /* name to process */
  972. /* Process a name or sh expression to operate on (or exclude).  Return
  973.    an error code in the ZE_ class. */
  974. {
  975.   char *a;              /* path and name for recursion */
  976.   dstrm *d;             /* directory stream from opend() */
  977.   char *e;              /* pointer to name from readd() */
  978.   struct flist far *f;  /* steps through found list */
  979.   int m;                /* matched flag */
  980.   char *p;              /* path for recursion */
  981.   struct stat s;        /* result of stat() */
  982.   struct zlist far *z;  /* steps through zfiles list */
  983.  
  984.   if (
  985. #ifdef S_IFLNK          /* if symbolic links exist ... */
  986.       linkput ? lstat(n, &s) :
  987. #endif /* S_IFLNK */
  988.       stat(n, &s)
  989. #ifdef __TURBOC__       /* Borland bug: stat() succeeds on wild card names! */
  990.       || isshexp(n)
  991. #endif /* __TURBOC__ */
  992.      )
  993.   {
  994.     /* Not a file--search for shell expression in zip file */
  995.     p = ex2in(n);               /* shouldn't affect matching chars */
  996.     m = 1;
  997.     for (z = zfiles; z != NULL; z = z->nxt)
  998.       if (MATCH(p, z->zname))
  999.       {
  1000.         z->mark = !exflag;
  1001.         if (verbose)
  1002.           printf("zip diagnostic: %scluding %s\n",
  1003.                  exflag ? "ex" : "in", z->name);
  1004.         m = 0;
  1005.       }
  1006.     /* If excluding, also search for expression in found list */
  1007.     if (exflag)
  1008.     {
  1009.       for (f = found; f != NULL;)
  1010.         if (MATCH(p, f->zname))
  1011.         {
  1012.           f = fexpel(f);
  1013.           m = 0;
  1014.         }
  1015.         else
  1016.           f = f->nxt;
  1017.     }
  1018.     free((voidp *)p);
  1019.     if (m)
  1020.       return ZE_MISS;           /* no match */
  1021.   }
  1022.   else
  1023.   {
  1024.     /* Live name--use if file, recurse if directory */
  1025. #ifdef MSDOS
  1026. #ifndef OS2
  1027.     strupr(n);                  /* convert to upper case */
  1028. #endif /* !OS2 */
  1029.     for (p = n; *p; p++)        /* use / consistently */
  1030.       if (*p == '\\')
  1031.         *p = '/';
  1032. #endif /* MSDOS */
  1033.     switch (s.st_mode & S_IFMT)
  1034.     {
  1035.       case S_IFREG:             /* add or remove name of file */
  1036. #ifdef S_IFLNK
  1037.       case S_IFLNK:
  1038. #endif /* S_IFLNK */
  1039.         if ((m = newname(n)) != ZE_OK)
  1040.           return m;
  1041.         break;
  1042.       case S_IFDIR:             /* recurse into directory */
  1043.         if (recurse && (d = opend(n)) != NULL)
  1044.         {
  1045. #ifdef VMS
  1046.           while ((e = readd(d)) != NULL)
  1047.             if ((m = procname(e)) != ZE_OK)     /* recurse on name */
  1048.             {
  1049.               /* want to just set warning error and continue */
  1050.               closed(d);
  1051.               return m;
  1052.             }
  1053. #else /* !VMS */
  1054.           if ((p = malloc(strlen(n)+2)) == NULL)
  1055.             return ZE_MEM;
  1056.           if (strcmp(n, ".") == 0)
  1057.             *p = 0;                     /* avoid "./" prefix */
  1058.           else
  1059.           {
  1060.             strcpy(p, n);
  1061.             a = p + strlen(p);
  1062.             if (a[-1] != '/')
  1063.               strcpy(a, "/");
  1064.           }
  1065.           while ((e = readd(d)) != NULL)
  1066.             if (strcmp(e, ".") && strcmp(e, ".."))
  1067.             {
  1068.               if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  1069.               {
  1070.                 free((voidp *)p);
  1071.                 closed(d);
  1072.                 return ZE_MEM;
  1073.               }
  1074.               strcat(strcpy(a, p), e);
  1075.               if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  1076.               {
  1077.                 free((voidp *)a);  free((voidp *)p);
  1078.                 closed(d);
  1079.                 return m;
  1080.               }
  1081.               free((voidp *)a);
  1082.             }
  1083.           free((voidp *)p);
  1084. #endif /* ?VMS */
  1085.           closed(d);
  1086.         }
  1087.       }
  1088.     }
  1089.   return ZE_OK;
  1090. }
  1091.  
  1092.  
  1093. #ifndef __TURBOC__
  1094. local int cmptime(p, q)
  1095. struct tm *p, *q;       /* times to compare */
  1096. /* Return negative if time p is before time q, positive if after, and
  1097.    zero if the same */
  1098. {
  1099.   int r;                /* temporary variable */
  1100.  
  1101.   if ((r = p->tm_year - q->tm_year) != 0)
  1102.     return r;
  1103.   else if ((r = p->tm_mon - q->tm_mon) != 0)
  1104.     return r;
  1105.   else if ((r = p->tm_mday - q->tm_mday) != 0)
  1106.     return r;
  1107.   else if ((r = p->tm_hour - q->tm_hour) != 0)
  1108.     return r;
  1109.   else if ((r = p->tm_min - q->tm_min) != 0)
  1110.     return r;
  1111.   else
  1112.     return p->tm_sec - q->tm_sec;
  1113. }
  1114.  
  1115.  
  1116. local time_t invlocal(t)
  1117. struct tm *t;           /* time to convert */
  1118. /* Find inverse of localtime() using bisection.  This routine assumes that
  1119.    time_t is an integer type, either signed or unsigned.  The expectation
  1120.    is that sometime before the year 2038, time_t will be made a 64-bit
  1121.    integer, and this routine will still work. */
  1122. {
  1123.   time_t i;             /* midpoint of current root range */
  1124.   time_t l;             /* lower end of root range */
  1125.   time_t u;             /* upper end of root range */
  1126.  
  1127.   /* Bracket the root [0,largest time_t].  Note: if time_t is a 32-bit signed
  1128.      integer, then the upper bound is GMT 1/19/2038 03:14:07, after which all
  1129.      the Unix systems in the world come to a grinding halt.  Either that, or
  1130.      all those systems will suddenly find themselves transported to December
  1131.      of 1901 ... */
  1132.   l = 0;
  1133.   u = 1;
  1134.   while (u < (u << 1))
  1135.     u = (u << 1) + 1;
  1136.  
  1137.   /* Find the root */
  1138.   while (u - l > 1)
  1139.   {
  1140.     i = l + ((u - l) >> 1);
  1141.     if (cmptime(localtime(&i), t) <= 0)
  1142.       l = i;
  1143.     else
  1144.       u = i;
  1145.   }
  1146.   return l;
  1147. }
  1148. #endif /* !__TURBOC__ */
  1149.  
  1150.  
  1151. void stamp(f, d)
  1152. char *f;                /* name of file to change */
  1153. ulg d;                  /* dos-style time to change it to */
  1154. /* Set last updated and accessed time of file f to the DOS time d. */
  1155. {
  1156. #ifdef __TURBOC__
  1157.   int h;                /* file handle */
  1158.  
  1159.   if ((h = open(f, 0)) != -1)
  1160.   {
  1161.     setftime(h, (struct ftime *)&d);
  1162.     close(h);
  1163.   }
  1164. #else /* !__TURBOC__ */
  1165. #ifdef VMS
  1166.   warn("timestamp not implemented yet under VMS", "");
  1167. #else /* !VMS */
  1168.   struct tm t;          /* argument for invlocal() */
  1169.   time_t u[2];          /* argument for utime() */
  1170.  
  1171.   /* Convert DOS time to time_t format in u[0] and u[1] */
  1172.   t.tm_sec = (int)(d << 1) & 0x3e;
  1173.   t.tm_min = (int)(d >> 5) & 0x3f;
  1174.   t.tm_hour = (int)(d >> 11) & 0x1f;
  1175.   t.tm_mday = (int)(d >> 16) & 0x1f;
  1176.   t.tm_mon = ((int)(d >> 21) & 0xf) - 1;
  1177.   t.tm_year = ((int)(d >> 25) & 0x7f) + 80;
  1178.   u[0] = u[1] = invlocal(&t);
  1179.  
  1180.   /* Set updated and accessed times of f */
  1181.   utime(f, u);
  1182. #endif /* ?VMS */
  1183. #endif /* ?__TURBOC__ */
  1184. }
  1185.  
  1186.  
  1187. local void inctime(s)
  1188. struct tm *s;           /* time to increment in place */
  1189. /* Increment the time structure *s by one second, return the result in
  1190.    place. */
  1191. {
  1192.   int y;                /* temporary variable */
  1193.  
  1194.   /* days in each month, except for February */
  1195.   static int days[] = {31,0,31,30,31,30,31,31,30,31,30,31};
  1196.  
  1197.   /* Set days in February from year (1900 is a leap year, 2000 is not) */
  1198.   y = s->tm_year + 1900;
  1199.   days[1] = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) ? 29 : 28;
  1200.  
  1201.   /* Increment time with carry */
  1202.   if (s->tm_sec != 59)
  1203.     s->tm_sec++;
  1204.   else if (s->tm_sec = 0, s->tm_min != 59)
  1205.     s->tm_min++;
  1206.   else if (s->tm_min = 0, s->tm_hour != 23)
  1207.     s->tm_hour++;
  1208.   else if (s->tm_hour = 0, s->tm_mday != days[s->tm_mon])
  1209.     s->tm_mday++;
  1210.   else if (s->tm_mday = 1, s->tm_mon != 11)
  1211.     s->tm_mon++;
  1212.   else
  1213.   {
  1214.     s->tm_mon = 0;
  1215.     s->tm_year++;
  1216.   }
  1217. }
  1218.  
  1219.  
  1220. ulg dostime(y, n, d, h, m, s)
  1221. int y;                  /* year */
  1222. int n;                  /* month */
  1223. int d;                  /* day */
  1224. int h;                  /* hour */
  1225. int m;                  /* minute */
  1226. int s;                  /* second */
  1227. /* Convert the date y/n/d and time h:m:s to a four byte DOS date and
  1228.    time (date in high two bytes, time in low two bytes allowing magnitude
  1229.    comparison). */
  1230. {
  1231.   return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) :
  1232.         (((ulg)y - 1980) << 25) | ((ulg)n << 21) | ((ulg)d << 16) |
  1233.         ((ulg)h << 11) | ((ulg)m << 5) | ((ulg)s >> 1);
  1234. }
  1235.  
  1236.  
  1237. local ulg unix2dostime(t)
  1238. time_t *t;              /* unix time to convert */
  1239. /* Return the Unix time t in DOS format, rounded up to the next two
  1240.    second boundary. */
  1241. {
  1242.   struct tm *s;         /* result of localtime() */
  1243.  
  1244.   s = localtime(t);             /* Use local time since MSDOS does */
  1245.   inctime(s);                   /* Add one second to round up */
  1246.   return dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
  1247.                  s->tm_hour, s->tm_min, s->tm_sec);
  1248. }
  1249.  
  1250.  
  1251. ulg filetime(f, a, n)
  1252. char *f;                /* name of file to get info on */
  1253. ulg *a;                 /* return value: file attributes */
  1254. long *n;                /* return value: file size */
  1255. /* If file *f does not exist, return 0.  Else, return the file's last
  1256.    modified date and time as an MSDOS date and time.  The date and
  1257.    time is returned in a long with the date most significant to allow
  1258.    unsigned integer comparison of absolute times.  Also, if a is not
  1259.    a NULL pointer, store the file attributes there, with the high two
  1260.    bytes being the Unix attributes, and the low byte being a mapping
  1261.    of that to DOS attributes.  If n is not NULL, store the file size
  1262.    there. */
  1263. {
  1264.   struct stat s;        /* results of stat() */
  1265.  
  1266. #ifdef S_IFLNK
  1267.   if (linkput ? lstat(f, &s) == 0 && ((s.st_mode & S_IFMT) == S_IFREG ||
  1268.                                       (s.st_mode & S_IFMT) == S_IFLNK) :
  1269. #else /* !S_IFLNK */
  1270.   if (
  1271. #endif /* ?S_IFLNK */
  1272.                 stat(f, &s) == 0 && (s.st_mode & S_IFMT) == S_IFREG)
  1273.   {
  1274.     if (a != NULL)
  1275.       *a = (s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  1276.     if (n != NULL)
  1277.       *n = s.st_size;
  1278. #ifdef VMS
  1279.     return unix2dostime(&s.st_ctime);   /* Use creation time in VMS */
  1280. #else /* !VMS */
  1281.     return unix2dostime(&s.st_mtime);
  1282. #endif /* ?VMS */
  1283.   }
  1284.   else
  1285.     return 0;
  1286. }
  1287.  
  1288.  
  1289. int issymlnk(a)
  1290. ulg a;                  /* Attributes returned by filetime() */
  1291. /* Return true if the attributes are those of a symbolic link */
  1292. {
  1293. #ifdef S_IFLNK
  1294.   return ((a >> 16) & S_IFMT) == S_IFLNK;
  1295. #else /* !S_IFLNK */
  1296.   return (int)a & 0;    /* avoid warning on unused parameter */
  1297. #endif /* ?S_IFLNK */
  1298. }
  1299.  
  1300.  
  1301. int deletedir(d)
  1302. char *d;                /* directory to delete */
  1303. /* Delete the (empty) directory *d.  Return the result of rmdir(), delete(),
  1304.    or system(). */
  1305. {
  1306. #ifdef RMDIR
  1307.   /* code from Greg Roelofs, who horked it from Mark Edwards (unzip) */
  1308.   int r, len;
  1309.   char *s;              /* malloc'd string for system command */
  1310.  
  1311.   len = strlen(d);
  1312.   if ((s = malloc(len + 34)) == NULL)
  1313.     return 127;
  1314.  
  1315. #ifdef VMS
  1316.   {
  1317.     char *c;            /* pointer into VMS path */
  1318.     /* convert "DEV:[DIR.SUB1.SUB2]" form to "DEV:[DIR.SUB1]SUB2.DIR;0" */
  1319.     strcat(strcpy(s, "set prot=(o:rwed) "), d);   /* d starts at s+18 */
  1320.     if (*(c = s+17+len) != ']')
  1321.     {
  1322.       free(s);
  1323.       return 127;
  1324.     }
  1325.     strcpy(c, ".DIR;0");        /* 0 translates to highest version */
  1326.     while (--c > s+18  &&  *c != '.'  &&  *c != '[') ;
  1327.     if (c == s+18)
  1328.     {
  1329.       free(s);
  1330.       return 127;
  1331.     }
  1332.     if (*c == '.')
  1333.       *c = ']';
  1334.     else if (*--c == ']')  /* presumably of form "DEV:[DIR.SUB1.][SUB2]" */
  1335.     {                      /* (possible to have "DEV:[DIR.SUB1.][][SUB2]"?) */
  1336.       char *b = c + 2;
  1337.       c[-1] = ']';
  1338.       while (*c++ = *b++) ;
  1339.     }
  1340.     else        /* must have reached device name:  can't delete top level */
  1341.     {
  1342.       free(s);
  1343.       return 127;
  1344.     }
  1345.   }
  1346.   /* unprotect directory and delete it as a file.  May fail if exists 
  1347.      normal file "foo.dir" on top of directory "foo.dir" */
  1348.   system(s);
  1349.   r = delete(s+18);
  1350. #else /* !VMS */
  1351.   sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);
  1352.   r = system(s);
  1353. #endif /* ?VMS */
  1354.   free(s);
  1355.   return r;
  1356. #else /* !RMDIR */
  1357.   return rmdir(d);
  1358. #endif /* ?RMDIR */
  1359. }
  1360.  
  1361.  
  1362. #endif /* !UTIL */
  1363.  
  1364.  
  1365. int destroy(f)
  1366. char *f;                /* file to delete */
  1367. /* Delete the file *f, returning non-zero on failure. */
  1368. {
  1369.   return unlink(f);
  1370. }
  1371.  
  1372.  
  1373. int replace(d, s)
  1374. char *d, *s;            /* destination and source file names */
  1375. /* Replace file *d by file *s, removing the old *s.  Return an error code
  1376.    in the ZE_ class. */
  1377. {
  1378.   struct stat t;        /* results of stat() */
  1379.  
  1380.   if (stat(d, &t) == 0 && unlink(d))
  1381.     return ZE_CREAT;                    /* Can't erase zip file--give up */
  1382.   if (link(s, d))                       /* Just move s on top of d */
  1383. #ifndef VMS                             /* For VMS, assume failure is EXDEV */
  1384.     if (errno != EXDEV)
  1385.       return ZE_CREAT;
  1386.     else
  1387. #endif /* !VMS */
  1388.     {
  1389.       FILE *f, *g;      /* source and destination files */
  1390.       int r;            /* temporary variable */
  1391.  
  1392.       if ((f = fopen(s, FOPR)) == NULL)
  1393.         return ZE_TEMP;
  1394.       if ((g = fopen(d, FOPW)) == NULL)
  1395.       {
  1396.         fclose(f);
  1397.         return ZE_CREAT;
  1398.       }
  1399.       r = fcopy(f, g, (ulg)-1L);
  1400.       fclose(f);
  1401.       if (fclose(g) || r != ZE_OK)
  1402.       {
  1403.         unlink(d);
  1404.         return r ? (r == ZE_TEMP ? ZE_WRITE : r) : ZE_WRITE;
  1405.       }
  1406. #ifdef VMS /* only delete if rename failed:  previous version may exist */
  1407.       unlink(s);
  1408.     }
  1409. #else /* !VMS */
  1410.     }
  1411.   unlink(s);
  1412. #endif /* !VMS */
  1413.   return ZE_OK;
  1414. }
  1415.  
  1416.  
  1417. int getfileattr(f)
  1418. char *f;                /* file path */
  1419. /* Return the file attributes for file f or -1 if failure */
  1420. {
  1421.   struct stat s;
  1422.  
  1423.   return stat(f, &s) == 0 ? s.st_mode : 0;
  1424. }
  1425.  
  1426.  
  1427. int setfileattr(f, a)
  1428. char *f;                /* file path */
  1429. int a;                  /* attributes returned by getfileattr() */
  1430. /* Give the file f the attributes a, return non-zero on failure */
  1431. {
  1432. #ifdef VMS
  1433.   return 0;
  1434. #else /* !VMS */
  1435.   return chmod(f, a);
  1436. #endif /* ?VMS */
  1437. }
  1438.  
  1439.  
  1440. char *tempname(c)
  1441. int c;                  /* character to insert in name */
  1442. /* Return a temporary file name in its own malloc'ed space, using tempath. */
  1443. {
  1444.   char *p;              /* temporary pointer */
  1445.   char *t;              /* malloc'ed space for name */
  1446.   
  1447.   if (tempath != NULL)
  1448.   {
  1449.     if ((t = malloc(strlen(tempath)+10)) == NULL)
  1450.       return NULL;
  1451.     strcpy(t, tempath);
  1452.     if (t[strlen(t)-1] != '/')
  1453.       strcat(t, "/");
  1454.   }
  1455.   else
  1456.   {
  1457.     if ((t = malloc(9)) == NULL)
  1458.       return NULL;
  1459.     *t = 0;
  1460.   }
  1461.   p = t + strlen(t);
  1462.   *p++ = '_';
  1463.   *p++ = (char)c;
  1464.   strcpy(p, "XXXXXX");
  1465.   return mktemp(t);
  1466. }
  1467.  
  1468.  
  1469. int fcopy(f, g, n)
  1470. FILE *f, *g;            /* source and destination files */
  1471. ulg n;                  /* number of bytes to copy or -1 for all */
  1472. /* Copy n bytes from file *f to file *g, or until EOF if n == -1.  Return
  1473.    an error code in the ZE_ class. */
  1474. {
  1475.   char *b;              /* malloc'ed buffer for copying */
  1476.   extent k;             /* result of fread() */
  1477.   ulg m;                /* bytes copied so far */
  1478.  
  1479.   if ((b = malloc(BSZ)) == NULL)
  1480.     return ZE_MEM;
  1481.   m = 0;
  1482.   while (n == -1L || m < n)
  1483.   {
  1484.     if ((k = fread(b, 1, n == -1 ?
  1485.                    BSZ : (n - m < BSZ ? (extent)(n - m) : BSZ), f)) == 0)
  1486.       if (ferror(f))
  1487.       {
  1488.         free((voidp *)b);
  1489.         return ZE_READ;
  1490.       }
  1491.       else
  1492.         break;
  1493.     if (fwrite(b, 1, k, g) != k)
  1494.     {
  1495.       free((voidp *)b);
  1496.       return ZE_TEMP;
  1497.     }
  1498.     m += k;
  1499.   }
  1500.   free((voidp *)b);
  1501.   return ZE_OK;
  1502. }
  1503.  
  1504.  
  1505. #ifndef EXPORT
  1506.  
  1507. #ifndef MSVMS
  1508.  
  1509. local int echofd = -1;  /* file descriptor whose echo is off */
  1510.  
  1511. void echoff(f)
  1512. int f;                  /* file descriptor to turn echo off on */
  1513. /* Turn echo off for file descriptor f.  Assumes that f is a tty device. */
  1514. {
  1515.   struct sgttyb sg;     /* tty device structure */
  1516.  
  1517.   echofd = f;
  1518.   GTTY(f, &sg);                                 /* get settings */
  1519.   sg.sg_flags &= ~ECHO;                         /* turn echo off */
  1520.   STTY(f, &sg);
  1521. }
  1522.  
  1523. void echon()
  1524. /* Turn echo back on for file descriptor echofd. */
  1525. {
  1526.   struct sgttyb sg;     /* tty device structure */
  1527.  
  1528.   if (echofd != -1)
  1529.   {
  1530.     GTTY(echofd, &sg);                          /* get settings */
  1531.     sg.sg_flags |= ECHO;                        /* turn echo on */
  1532.     STTY(echofd, &sg);
  1533.     echofd = -1;
  1534.   }
  1535. }
  1536.  
  1537. #endif /* !MSVMS */
  1538.  
  1539.  
  1540. char *getp(m, p, n)
  1541. char *m;                /* prompt for password */
  1542. char *p;                /* return value: line input */
  1543. int n;                  /* bytes available in p[] */
  1544. /* Get a password of length n-1 or less into *p using the prompt *m.
  1545.    The entered password is not echoed.  Return p on success, NULL on
  1546.    failure (can't get controlling tty). */
  1547. {
  1548.   char c;               /* one-byte buffer for read() to use */
  1549.   int i;                /* number of characters input */
  1550.   char *w;              /* warning on retry */
  1551.  
  1552. #ifndef MSVMS
  1553.   int f;                /* file decsriptor for tty device */
  1554.  
  1555.   /* Turn off echo on tty */
  1556.   if (!isatty(2))
  1557.     return NULL;                                /* error if not tty */
  1558.   if ((f = open(ttyname(2), 0, 0)) == -1)
  1559.     return NULL;
  1560.   echoff(f);                                    /* turn echo off */
  1561. #endif /* !MSVMS */
  1562.  
  1563.   /* Get password */
  1564.   w = "";
  1565.   do {
  1566.     fputs(w, stderr);                           /* warning if back again */
  1567.     fputs(m, stderr);                           /* prompt */
  1568.     fflush(stderr);
  1569.     i = 0;
  1570.     do {                                        /* read line, keeping n */
  1571. #ifdef MSVMS
  1572.       if ((c = (char)getch()) == '\r')
  1573.         c = '\n';
  1574. #else /* !MSVMS */
  1575.       read(f, &c, 1);
  1576. #endif /* ?MSVMS */
  1577.       if (i < n)
  1578.         p[i++] = c;
  1579.     } while (c != '\n');
  1580.     putc('\n', stderr);  fflush(stderr);
  1581.     w = "(line too long--try again)\n";
  1582.   } while (p[i-1] != '\n');
  1583.   p[i-1] = 0;                                   /* terminate at newline */
  1584.  
  1585. #ifndef MSVMS
  1586.   /* Turn echo back on */
  1587.   echon();                                      /* turn echo back on */
  1588.   close(f);
  1589. #endif /* !MSVMS */
  1590.  
  1591.   /* Return pointer to password */
  1592.   return p;
  1593. }
  1594.  
  1595. #endif /* !EXPORT */
  1596.  
  1597.  
  1598. #ifdef ZMEM
  1599.  
  1600. /************************/
  1601. /*  Function memset()  */
  1602. /************************/
  1603.  
  1604. /*
  1605.  * memset - for systems without it
  1606.  *  bill davidsen - March 1990
  1607.  */
  1608.  
  1609. char *
  1610. memset(buf, init, len)
  1611. register char *buf;     /* buffer loc */
  1612. register int init;      /* initializer */
  1613. register unsigned int len;   /* length of the buffer */
  1614. {
  1615.     char *start;
  1616.  
  1617.     start = buf;
  1618.     while (len--) *(buf++) = init;
  1619.     return(start);
  1620. }
  1621.  
  1622.  
  1623. /************************/
  1624. /*  Function memcpy()  */
  1625. /************************/
  1626.  
  1627. char *
  1628. memcpy(dst,src,len)           /* v2.0f */
  1629. register char *dst, *src;
  1630. register unsigned int len;
  1631. {
  1632.     char *start;
  1633.  
  1634.     start = dst;
  1635.     while (len--)
  1636.         *dst++ = *src++;
  1637.     return(start);
  1638. }
  1639.  
  1640.  
  1641. /************************/
  1642. /*  Function memcmp()  */
  1643. /************************/
  1644.  
  1645. int
  1646. memcmp(b1,b2,len)                     /* jpd@usl.edu -- 11/16/90 */
  1647. register char *b1, *b2;
  1648. register unsigned int len;
  1649. {
  1650.  
  1651.     if (len) do {             /* examine each byte (if any) */
  1652.       if (*b1++ != *b2++)
  1653.         return (*--((uch *)b1) - *--((uch *)b2));  /* exit when miscompare */
  1654.        } while (--len);
  1655.  
  1656.     return(0);        /* no miscompares, yield 0 result */
  1657. }
  1658.  
  1659. #endif  /* ZMEM */
  1660.